package Sort;

/**
 * @author shy_black
 * @date 2019/8/2 17:09
 * @Description:
 */
public class 希尔排序 {

    public static void shellSort(int[] arr) {
        int n = arr.length;
        int i, j, gap;

        for (gap = n / 2; gap > 0; gap /= 2) //步长
            for (i = 0; i < gap; i++)        //直接插入排序
            {
                for (j = i + gap; j < n; j += gap)
                    if (arr[j] < arr[j - gap]) {
                        int temp = arr[j];
                        int k = j - gap;
                        while (k >= 0 && arr[k] > temp) {
                            arr[k + gap] = arr[k];
                            k -= gap;
                        }
                        arr[k + gap] = temp;
                    }
            }


    }


    private static void swap(int[] arr, int i, int j) {
        int t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1,3,5,7,9,2,4,6,8,0};
        shellSort(arr);
        for (int i : arr) {
            System.out.print(i + "-");
        }
    }
}